package com.hzy.pay.config;

import com.alipay.api.*;
import com.alipay.api.request.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/***
 * @author huangzhaoyong
 * @desc 支付宝支付
 * @date  2019/3/4 0004 下午 14:54
 */
public class AliPayUtils {
    private static Logger logger = LoggerFactory.getLogger(AliPayUtils.class);

    private static AlipayClient alipayClient;

    static {
        alipayClient = new DefaultAlipayClient(AliPayProperties.serverUrl, AliPayProperties.appId,
                AliPayProperties.appPrivateKey, AliPayProperties.format, AliPayProperties.charset,
                AliPayProperties.alipayPublicKey, AliPayProperties.signType);
    }

    /**
     * 当面付 ： 条码支付
     * @param orderNo  商户订单id
     * @param payCode  用户支付码
     * @param title    订单标题
     * @param storeId  商户id
     * @param totalAmount 总金额
     * @return  支付结果
     */
    public static String barcodePay(String orderNo, String  payCode, String title, String storeId, String totalAmount) {
        //创建API对应的request类
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+orderNo+"\"," +
                "    \"scene\":\"bar_code\"," +
                "    \"auth_code\":\""+payCode+"\"," +
                "    \"subject\":\""+title+"\"," +
                "    \"store_id\":\""+storeId+"\"," +
                "    \"timeout_express\":\"2m\"," +
                "    \"total_amount\":\""+totalAmount+"\"" +
                "  }"); //设置业务参数
        return getResponseBody(request);
    }



    /**
     * 当面付 ： 条码支付
     * @param orderNo  商户订单id
     * @param payCode  用户支付码
     * @param title    订单标题
     * @param storeId  商户id
     * @param totalAmount 总金额
     * @return  支付结果
     */
    public static String barcodePay(String orderNo, String  payCode, String title, String storeId, String totalAmount,
                                    String sys_service_provider_id, String timeoutExpress) {
        //创建API对应的request类
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        //设置业务参数
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+orderNo+"\"," +
                "    \"scene\":\"bar_code\"," +
                "    \"auth_code\":\""+payCode+"\"," +
                "    \"subject\":\""+title+"\"," +
                "    \"store_id\":\""+storeId+"\"," +
                "    \"timeout_express\":\""+timeoutExpress+"\"," +
                "    \"extend_params\":{" +
                "    \"sys_service_provider_id\":\""+sys_service_provider_id+"\"" +
                "    }," +
                "    \"total_amount\":\""+totalAmount+"\"" +
                "  }");
        return getResponseBody(request);
    }

    /**
     * 订单查询接口
     * @param orderNo   订单号
     * @return  订单详细
     */
    public static String queryOrder(String orderNo) {
        //创建API对应的request类
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //设置业务参数
        request.setBizContent("{" +
                "\"out_trade_no\":\""+orderNo+"\""+
                "  }");
        return getResponseBody(request);
    }

    /**
     * 退款接口
     
     * @param orderNo   订单号
     * @param refundAmount  退款金额
     * @return
     */
    public static String refundOrder(String orderNo, String out_request_no, String refundAmount) {
        //创建API对应的request类
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        //设置业务参数
        request.setBizContent("{" +
                "\"out_trade_no\":\""+orderNo+"\"," +
                "\"out_request_no\":\""+out_request_no+"\"," +
                "\"refund_amount\":"+refundAmount+"" +
                "  }");
        return getResponseBody(request);
    }


    /**
     * 关闭订单
     * @param orderNo   订单号
     * @return  订单信息
     */
    public static String closeOrder(String orderNo,String authToken) {
        //创建API对应的request类
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        //设置业务参数
        request.setBizContent("{" +
                "\"out_trade_no\":\""+orderNo+"\"" +
                "  }");
        return getResponseBody(request);
    }


    /**
     * 撤销订单
     * @param orderNo   订单号
     * @return  订单信息
     */
    public static String cancelOrder(String orderNo,String authToken) {
        //创建API对应的request类
        AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
        //设置业务参数
        request.setBizContent("{" +
                "\"out_trade_no\":\""+orderNo+"\"" +
                "  }");
        return getResponseBody(request);
    }



    /**
     * 通过code获取token
     * @param code   code
     * @return  订单信息
     */
    public static String getAccessToken(String code) {
        //创建API对应的request类
        AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
        //设置业务参数
        request.setBizContent("{" +
                "    \"grant_type\":\"authorization_code\"," +
                "    \"code\":\""+code+"\"" +
                "  }");
        return getResponseBody(request);
    }


    /**
     * 刷新token
     * @param token   token
     * @return  订单信息
     */
    public static String refreshAccessToken(String token) {
        //创建API对应的request类
        AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
        //设置业务参数
        request.setBizContent("{" +
                "    \"grant_type\":\"refresh_token\"," +
                "    \"refresh_token\":\""+token+"\"" +
                "  }");
        return getResponseBody(request);
    }

    /**
     * 查询订单状态
     * @param token token
     * @return
     */
    public static String queryAccessToken(String token) {
        //创建API对应的request类
        AlipayOpenAuthTokenAppQueryRequest request = new AlipayOpenAuthTokenAppQueryRequest();
        //设置业务参数
        request.setBizContent("{" +
                "    \"app_auth_token\":\""+token+"\"" +
                "  }");
        return getResponseBody(request);
    }

    private static String getResponseBody(AlipayRequest request) {
        AlipayResponse response;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            logger.error(e.getMessage());
            return null;
        }
        return response.getBody();
    }
}
